import { http } from "@/utils/http";

type Result = {
  success: boolean;
  data: any;
};

type ResultTable = {
  success: boolean;
  data?: {
    list: Array<any>;
    data: Array<any>;
    total?: number;
    pageSize?: number;
    currentPage?: number;
  };
};

// 任务类型管理API
export const taskTypeApi = {
  // 获取任务类型列表
  list: (data?: object) => {
    const params = data ? `?${new URLSearchParams(data as any).toString()}` : '';
    return http.request<ResultTable>('get', `/task/types${params}`, { data });
  },
  
  // 创建任务类型
  create: (data: object) => {
    return http.request<Result>('post', '/task/types', { data });
  },
  
  // 更新任务类型
  update: (id: number, data: object) => {
    return http.request<Result>('put', `/task/types/${id}`, { data });
  },
  
  // 删除任务类型
  delete: (id: number) => {
    return http.request<Result>('delete', `/task/types/${id}`);
  },
  
  // 获取任务类型选项
  options: () => {
    return http.request<Result>('get', '/task/types/options');
  }
};

// 任务策略管理API
export const taskStrategyApi = {
  // 获取策略列表
  list: (data?: object) => {
    const params = data ? `?${new URLSearchParams(data as any).toString()}` : '';
    return http.request<ResultTable>('get', `/task/strategies${params}`, { data });
  },
  
  // 创建策略
  create: (data: object) => {
    return http.request<Result>('post', '/task/strategies', { data });
  },
  
  // 更新策略
  update: (id: number, data: object) => {
    return http.request<Result>('put', `/task/strategies/${id}`, { data });
  },
  
  // 删除策略
  delete: (id: number) => {
    return http.request<Result>('delete', `/task/strategies/${id}`);
  },
  
  // 启用/禁用策略
  enable: (id: number) => {
    return http.request<Result>('put', `/task/strategies/${id}/enable`);
  },
  
  // 预览策略效果
  preview: (id: number) => {
    return http.request<Result>('get', `/task/strategies/${id}/preview`);
  },
  
  // 手动触发任务分配
  manualAssign: () => {
    return http.request<Result>('post', '/task/strategies/manual-assign');
  },
  
  // 批量执行选中策略
  batchExecute: (strategyIds: number[]) => {
    return http.request<Result>('post', '/task/strategies/batch-execute', { 
      data: { strategy_ids: strategyIds } 
    });
  }
};

// 客户任务管理API
export const customerTaskApi = {
  // 获取任务列表
  list: (data?: object) => {
    const params = data ? `?${new URLSearchParams(data as any).toString()}` : '';
    return http.request<ResultTable>('get', `/task/customer-tasks${params}`, { data });
  },
  
  // 创建任务
  create: (data: object) => {
    return http.request<Result>('post', '/task/customer-tasks', { data });
  },
  
  // 更新任务
  update: (id: number, data: object) => {
    return http.request<Result>('put', `/task/customer-tasks/${id}`, { data });
  },
  
  // 删除任务
  delete: (id: number) => {
    return http.request<Result>('delete', `/task/customer-tasks/${id}`);
  },
  
  // 批量删除任务
  batchDestroy: (data: { ids: number[] }) => {
    return http.request<Result>('post', '/task/customer-tasks/batch-destroy', { data });
  },
  
  // 开始任务
  start: (id: number) => {
    return http.request<Result>('post', `/task/customer-tasks/${id}/start`);
  },
  
  // 完成任务
  complete: (id: number, data: object) => {
    return http.request<Result>('post', `/task/customer-tasks/${id}/complete`, { data });
  },
  
  // 手动分配任务
  assign: (data: object) => {
    return http.request<Result>('post', '/task/assign', { data });
  },
  
  // 获取任务统计
  statistics: (data?: object) => {
    const params = data ? `?${new URLSearchParams(data as any).toString()}` : '';
    return http.request<Result>('get', `/task/statistics${params}`, { data });
  },
  
  // 获取客户的任务列表
  getCustomerTasks: (customerId: number) => {
    return http.request<Result>('get', `/task/customer-tasks/customer/${customerId}`);
  }
};